#!/usr/bin/python
# encoding: utf-8
'''
@File    :   ghost_code.py
@Time    :   2020/03/11 21:54:19
@Author  :   fangkaijin 
@Version :   1.0
@Contact :   fangkaijin@gmail.com
@License :   杭州移动互联
@Desc    :   None
'''

# 导入库包

import sys, os
from _collections_abc import Iterable, Iterator

def fact(n):
    if n==1:
        return 1
    return n*fact(n-1)

print(fact(7))

#切片
l = ['小黑','小白','小红','小黄','小丁','小吖','小方','小王']
print(l)
print(l[0:2])
print(l[:3])

lr = list(range(100))
print(lr)
print(lr[:10])

t = tuple(range(100))
print(t)
print(t[:5])

s = str(1234567890)
print(s)
print(s[1:5])

print(isinstance(s, Iterable))

#遍历list 取到下标和值
l = ['小黑', '小白', '小绿', '小黄', '小红']
for index, value in enumerate(l):
    print(index, value)

#列表生成
l = [x * x for x in range(1,10)]
print(l)

ls = [x * x for x in range(1,10) if x % 2 == 0]
print(ls)

mn = [m + n for m in 'ABC' for n in "EFG"]
print(mn)

k = [x if x % 2 != 0 else x for x in range(1, 10)]
print(k)

#生成器
lg = (x * x for x in range(0,10))
for n in lg:
    print(n)

def feb(max):
    n, a, b = 0, 0, 1
    while n < max:
        yield b
        a, b = b, a+b
        n = n+1
    return 'done'

print(feb(6))

for n in feb(6):
    print(n)

#输入生成器的retrun done
g = feb(10)
while True:
    try:
        x = next(g)
        print('g:', x)
    except StopIteration as e:
        print('Generator return value:', e.value)
        break

print(isinstance(list(range(1,10)), Iterator))
print(isinstance((x *x for x in range(1,10)), Iterator))

print(abs(-10))

#高阶函数
def iabs(x, y, z):
    return z(x) + z(y)

print(iabs(9, -8, abs))

def f(x):
    return x * x

r = map(f, list(range(0, 10)))
print(list(r))

print(list(map(str, list(range(0, 10)))))

from functools import reduce

def f(x, y):
    return x * 10 + y

print(reduce(f, [1, 3, 5, 7, 9]))

DIGITS = {'0': 0, '1': 1, '2': 2, '3': 3, '4': 4, '5': 5, '6': 6, '7': 7, '8': 8, '9': 9}
#
def strToint(s):
    def fn(x, y):
        return x * 10 + y
    def charToNum(s):
        return DIGITS[s]
    return reduce(fn, map(charToNum, s))
print(strToint('3'))

#lambal形式
def char2NUm(s):
    return DIGITS[s]
def str2Int(s):
    return reduce(lambda x, y: x*10+y, map(char2NUm, s))

print(strToint('110'))